package com.ltsznh.android.locationreporting.sync;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.ltsznh.android.locationreporting.db.dbo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 涛 on 2014/12/15.
 */
public class SyncThread extends Thread {
    private Context context;

    Cursor cursor;

    public void stopSync() {
        Log.v("dddd", "stop sync");
        cursor.close();
    }

    public SyncThread(Context context) {
        this.context = context;
    }

    @Override
    public void run() {
        Log.v("dddd", "thread run");
        int count = 0;

        int rows = dbo.rawQuery(context, "UPDATE lr_details" +
                "   SET isSync = 'O'" +
                " WHERE isSync = 'N';", null);

        dbo.getReadableDatabase(context);

        cursor = dbo.fbSQLite.rawQuery("SELECT id," +
                "       IFNULL(userCode,'') AS userCode ," +
                "       IFNULL(userName,'') AS userName," +
                "       IFNULL(phoneId,'') AS phoneId," +
                "       IFNULL(phoneName,'') AS phoneName," +
                "       IFNULL(provider,'') AS provider," +
                "       IFNULL(reportDateTime,'') AS reportDateTime," +
                "       IFNULL(reportDate,'') AS reportDate," +
                "       IFNULL(reportTime,'') AS reportTime," +
                "       IFNULL(latitude,'') AS latitude," +
                "       IFNULL(longitude,'') AS longitude," +
                "       IFNULL(accuracy,'') AS accuracy," +
                "       IFNULL(altitude,'') AS altitude," +
                "       IFNULL(bearing,'') AS bearing," +
                "       IFNULL(speed,'') AS speed," +
                "       IFNULL(entryDate,'') AS entryDate," +
                "       IFNULL(entryTime,'') AS entryTime," +
                "       IFNULL(addressName,'') AS addressName" +
                "  FROM lr_details" +
                " WHERE isSync = 'O' " +
                " ORDER BY reportDateTime" +
                ";", null);
        Log.v("dddd", "sync data count:" + cursor.getCount());
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {
                Map<String, String> map = new HashMap<String, String>();
                map.put("class", "LrSync");
                map.put("method", "details");

                map.put("id", cursor.getString(cursor.getColumnIndex("id")));
                map.put("userCode", cursor.getString(cursor.getColumnIndex("userCode")));
                map.put("userName", cursor.getString(cursor.getColumnIndex("userName")));
                map.put("phoneId", cursor.getString(cursor.getColumnIndex("phoneId")));
                map.put("phoneName", cursor.getString(cursor.getColumnIndex("phoneName")));
                map.put("provider", cursor.getString(cursor.getColumnIndex("provider")));
                map.put("reportDateTime", cursor.getString(cursor.getColumnIndex("reportDateTime")));
                map.put("reportDate", cursor.getString(cursor.getColumnIndex("reportDate")));
                map.put("reportTime", cursor.getString(cursor.getColumnIndex("reportTime")));
                map.put("latitude", cursor.getString(cursor.getColumnIndex("latitude")));
                map.put("longitude", cursor.getString(cursor.getColumnIndex("longitude")));
                map.put("accuracy", cursor.getString(cursor.getColumnIndex("accuracy")));
                map.put("altitude", cursor.getString(cursor.getColumnIndex("altitude")));
                map.put("bearing", cursor.getString(cursor.getColumnIndex("bearing")));
                map.put("speed", cursor.getString(cursor.getColumnIndex("speed")));
                map.put("entryDate", cursor.getString(cursor.getColumnIndex("entryDate")));
                map.put("entryTime", cursor.getString(cursor.getColumnIndex("entryTime")));
                map.put("addressName", cursor.getString(cursor.getColumnIndex("addressName")));
                try {
                    String result = SyncData.sendHttpClientPost("http://192.168.11.110:8080/family_system/sync", map, "utf-8");
                    if (result != null && !result.equals("")) {
                        ContentValues values = new ContentValues();
                        values.put("isSync", "Y");
                        dbo.updateData(context, "lr_details", values, "id = ? AND isSync = 'O'", new String[]{result});
                        Log.v("dddd", "sync:" + result);
                        count++;
//                        Thread.sleep(1000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    cursor.close();
                }
            } while (!cursor.isClosed() && cursor.moveToNext());
        }
        Log.v("dddd", "sync:" + count);
        cursor.close();
        dbo.closeDatabase();
    }
}
